//
//  HomeViewController.swift
//  yueshijia
//
//  Created by 王磊 on 17/4/18.
//  Copyright © 2017年 王磊. All rights reserved.
//

import UIKit

class HomeViewController: UIViewController {
    var tempArr = Array<String>()
    var homeCellArray = Array<HomeThingCellModel>()
    var infoString:String?
    var infoPicName:String?
    var receiverIdentifier    = "homeCell"
    

    lazy var tableView:UITableView = {
        let table = UITableView.init(frame: CGRect.init(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT), style: UITableViewStyle.grouped)
        table.delegate = self;
        table.dataSource = self;
        
        let requesterNib = UINib(nibName: "ReceiverListCell", bundle: nil)
        table.register(requesterNib, forCellReuseIdentifier: receiverIdentifier)
        return table
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        loadData()
        self.view.backgroundColor = UIColor.white
        self.view.addSubview(tableView)
        
    }
    func loadData() {
        DispatchQueue.global().async {
             let path = Bundle.main.path(forResource: "Home", ofType: "json")!
            // 2.通过文件路径创建NSData
             let jsonData = NSData.init(contentsOfFile: path)
           // 带throws的方法需要抛异常
                do {
                    /*
                     * try 和 try! 的区别
                     * try 发生异常会跳到catch代码中
                     * try! 发生异常程序会直接crash
                     */
                    let json = try JSONSerialization.jsonObject(with: jsonData! as Data, options: JSONSerialization.ReadingOptions.allowFragments)
                    var listData: Dictionary<String, Any>
                    listData = json as! Dictionary<String, Any>
                    var datas: Dictionary<String, Any>
                    datas = listData["datas"] as! Dictionary<String, Any>
                    var bannerArr = Array<Any>()
                    bannerArr = datas["banner"] as! Array<Any>

                    for i in 0..<bannerArr.count {
                         let tempDic: Dictionary<String, Any>
                         tempDic = bannerArr[i] as! Dictionary<String, Any>
                         let pic = tempDic["advertImg"]
                       
                         self.tempArr.append(pic as! String)
                    }
                    var dataType = Array<Any>()
                    dataType = datas["data_type"] as! Array<Any>
                    // 字典数组 -> 模型数组
                    for i in 0..<dataType.count {
                        var tempDict: Dictionary<String, Any>
                        tempDict = dataType[i] as! Dictionary<String, Any>
                        let status = HomeThingCellModel.init(dic: tempDict)
                        
                     self.homeCellArray.append(status)
                    }
                    let  firstData : Dictionary<String ,Any> = dataType[0] as!Dictionary<String, Any>
                   
                    self.infoString = firstData["relation_object_title"] as! String?
                    self.infoPicName = firstData["relation_object_image"] as! String?
                    DispatchQueue.main.async {
                        self.tableView.reloadData()
                    }
                    
                }catch {
                    // 异常代码放在这
                    print(error)
                }
    }
  
    }
}
extension HomeViewController:UITableViewDelegate,UITableViewDataSource{
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.homeCellArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell:HomeThingCell?  = tableView.dequeueReusableCell(withIdentifier: receiverIdentifier) as? ReceiverListCell
        
//        cell.setDatas(model: homeCellArray[indexPath.row])
        return cell
    }
    
    
    
    
    
    
}
